package com.xialinrui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xialinrui.entity.Desire;
import com.xialinrui.entity.Expenditure;
import com.xialinrui.entity.ResponseResult;
import com.xialinrui.entity.User;
import com.xialinrui.service.DesireService;
import com.xialinrui.service.ExpenditureService;
import com.xialinrui.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("/desire")
public class DesireController {


    @Resource
    private DesireService desireService;

    @Resource
    private UserService userService;

    @Resource
    private ExpenditureService expenditureService;


    /**
     * 添加一个欲望
     *
     * @param desire
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Map<String, Long>> addDesire(@RequestBody Desire desire) {
        QueryWrapper<Desire> desireQueryWrapper = new QueryWrapper<>();
        desireQueryWrapper.eq("name", desire.getName());
        Desire des = desireService.getOne(desireQueryWrapper);
        if (des != null) {
            return new ResponseResult(400, "已经添加过该欲望啦~");
        }
        HashMap<String, Long> resMap = new HashMap<>();
        desireService.addDesire(desire);
        resMap.put("id", desire.getId());
        return new ResponseResult(200, "添加成功~", resMap);
    }

    /**
     * 兑换一个欲望
     *
     * @param id
     * @param way
     * @return
     */
    @PutMapping("/redeem")
    public ResponseResult redeem(@RequestParam Long id, @RequestParam String way) {
        QueryWrapper<Desire> desireQueryWrapper = new QueryWrapper<>();
        desireQueryWrapper.eq("id", id);
        Desire desire = desireService.getOne(desireQueryWrapper);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id", 1);
        User user = userService.getOne(userQueryWrapper);
        if (desire.getLoveToSpend() == 0 && desire.getMoneyToSpend() == 0) {
            return new ResponseResult(400, "你还没有设置兑换价值哦~");
        }
        if ("money".equals(way)) {
            if (userService.getAllMoney() >= desire.getMoneyToSpend()) {
                UpdateWrapper<Desire> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("is_redeemed", 1).set("real_money_spent", desire.getMoneyToSpend()).set("redeem_time", new Date()).eq("id", id);
                desireService.update(updateWrapper);
                //创建一条支出
                Expenditure expenditure = new Expenditure(null, desire.getUpdateTime(), 2L, desire.getName(), desire.getMoneyToSpend());
                //添加一条支出记录
                expenditureService.addExpenditure(expenditure);
            } else {
                return new ResponseResult(400, "您的钱数不够哦，还得努力加油～");
            }
        } else {
            if (userService.getAllLove() >= desire.getLoveToSpend()) {
                UpdateWrapper<Desire> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("is_redeemed", 1).set("real_love_spent", desire.getLoveToSpend()).set("redeem_time", new Date()).eq("id", id);
                desireService.update(updateWrapper);
                UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
                userUpdateWrapper.set("whole_love", user.getWholeLove() - desire.getLoveToSpend()).eq("user_id", 1);
                userService.update(userUpdateWrapper);
            } else {
                return new ResponseResult(400, "您的爱心不够哦，还得努力加油～");
            }
        }
        return new ResponseResult(200, "兑换成功～");
    }

    /**
     * 删除欲望
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Long> ids) {
        desireService.deleteByIds(ids);
        return new ResponseResult(200, "删除成功");
    }


    /**
     * 查询未兑换的欲望（根据更新时间从近到远）
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/not/redeemed")
    public ResponseResult<Page<Desire>> listNotRedeemed(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<Desire> resPage = new Page<>(page, pageSize);
        QueryWrapper<Desire> desireQueryWrapper = new QueryWrapper<>();
        desireQueryWrapper.eq("is_redeemed", 0).orderByDesc("update_time");
        desireService.page(resPage, desireQueryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    /**
     * 查询已兑换的欲望（根据兑换时间从近到远）
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/redeemed")
    public ResponseResult<Page<Desire>> listRedeemed(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<Desire> resPage = new Page<>(page, pageSize);
        QueryWrapper<Desire> desireQueryWrapper = new QueryWrapper<>();
        desireQueryWrapper.eq("is_redeemed", 1).orderByDesc("redeem_time");
        desireService.page(resPage, desireQueryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }


    /**
     * 根据输入框查询欲望
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public ResponseResult<Page<Desire>> search(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam String content) {
        Page<Desire> resPage = new Page<>(page, pageSize);
        QueryWrapper<Desire> desireQueryWrapper = new QueryWrapper<>();
        desireQueryWrapper.like("name", content);
        desireService.page(resPage, desireQueryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    /**
     * 根据时间段查询已兑换的欲望
     *
     * @param page
     * @param pageSize
     * @param from
     * @param to
     * @return
     */
    @GetMapping("redeemed/by/time")
    public ResponseResult<Page<Desire>> listRedeemedByTime(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam java.sql.Date from, @RequestParam java.sql.Date to) {
        Page<Desire> resPage = new Page<>(page, pageSize);
        QueryWrapper<Desire> desireQueryWrapper = new QueryWrapper<>();
        desireQueryWrapper.eq("is_redeemed", 1).between("redeem_time", from, to).orderByDesc("redeem_time");
        desireService.page(resPage, desireQueryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }


    /**
     * 根据时间段查询未兑换的欲望
     *
     * @param page
     * @param pageSize
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/not/redeemed/by/time")
    public ResponseResult<Page<Desire>> listNotRedeemedByTime(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam java.sql.Date from, @RequestParam java.sql.Date to) {
        Page<Desire> resPage = new Page<>(page, pageSize);
        QueryWrapper<Desire> desireQueryWrapper = new QueryWrapper<>();
        desireQueryWrapper.eq("is_redeemed", 0).between("update_time", from, to).orderByDesc("update_time");
        desireService.page(resPage, desireQueryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

}
